//
//  MSSweepCodeActivityController.m
//  觅尚
//
//  Created by No.8 on 2019/4/3.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSSweepCodeActivityController.h"
#import "MSDayActivityController.h"
#import "MSMonthActivityController.h"

@interface MSSweepCodeActivityController ()
{
    CustomNavigationBar *naviagtionBar;
}
/** 选择按钮*/
@property(nonatomic,strong)UIButton *selectedButton;

/** 底部所有的标签 */
@property (nonatomic, strong) UIView  * titlesView;

/** 底部所有的内容 */
@property (nonatomic, strong) UIScrollView  * contenView;
@end

@implementation MSSweepCodeActivityController

- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    

}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self CreateNavigationBar];
    
    
    [self CreatView];
    
    
    //初始化子控制器
    [self setupChildVces];
    
    
    [self setupScrollView];
    
}
- (void)CreatView{
    // 标签栏整体
    UIView *titlesView = [[UIView alloc] init];
    titlesView.backgroundColor = [UIColor clearColor];
    titlesView.frame = CGRectMake(ScreenWidth * 0.25, [self navBarBottom] + 40 * px, ScreenWidth * 0.5, 90 * px);
    [self.view addSubview:titlesView];
    self.titlesView = titlesView;
    
    
    NSArray * titleArray = @[@"日统计",@"月统计"];
    
    CGFloat ButtonWidth = titlesView.width * 0.5;
    CGFloat ButtonHeight = titlesView.height;
    CGFloat ButtonY = 0;
    for (NSInteger i = 0; i < titleArray.count; i ++) {
        UIButton * Button = [[UIButton alloc] init];
        Button.tag = i;
        [Button layoutIfNeeded];//强制布局
        Button.x = ButtonWidth * i;
        Button.y = ButtonY;
        Button.width = ButtonWidth;
        Button.height = ButtonHeight;
        [Button setTitle:titleArray[i] forState:UIControlStateNormal];
        [Button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [Button setTitleColor:[UIColor blackColor] forState:UIControlStateDisabled];
        [Button addTarget:self action:@selector(ButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [Button setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]] forState:UIControlStateNormal];
        [Button setBackgroundImage:[UIImage imageWithColor:WYCRGBColor(95, 207, 185)] forState:UIControlStateDisabled];
        Button.layer.cornerRadius = 45 * px;
        Button.layer.masksToBounds = YES;
        Button.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
        //        [Button setBackgroundColor:BackViewColor];
        [titlesView addSubview:Button];
        // 默认点击了第一个按钮
        if (i == 0) {
            [self ButtonClick:Button];
            Button.enabled = NO;
            
            self.selectedButton = Button;
        }
    }
    
}
- (void)ButtonClick:(UIButton *)button{
    //修改按钮状态
    self.selectedButton.enabled = YES;
    button.enabled = NO;
    self.selectedButton = button;
    
    //滚动
    CGPoint offset = self.contenView.contentOffset;
    offset.x = button.tag * self.contenView.width;
    [self.contenView setContentOffset:offset animated:YES];
    
}
/**
 *底部的scrollview
 */
- (void)setupScrollView
{
    //不自动调整inset
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIScrollView * contenView = [[UIScrollView alloc]init];
    contenView.frame = self.view.bounds;
    contenView.delegate = self;
    contenView.pagingEnabled = YES;
    
    [self.view insertSubview:contenView atIndex:0];
    contenView.contentSize = CGSizeMake(contenView.width * self.childViewControllers.count,0);
    
    self.contenView = contenView;
    //
    //    //添加第一个控制器view
    [self scrollViewDidEndScrollingAnimation:contenView];
}

- (void)setupChildVces{
    MSDayActivityController * DayVC = [[MSDayActivityController alloc] init];
    [self addChildViewController:DayVC];

    MSMonthActivityController * MonthVC = [[MSMonthActivityController alloc] init];
    [self addChildViewController:MonthVC];
    
}
#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    
    //当前的索引
    //长度NSInteger * interger;
    int index = scrollView.contentOffset.x / scrollView.width;
    
    //取出子控制器
    UIViewController * vc = self.childViewControllers[index];
    vc.view.x = scrollView.contentOffset.x;
    vc.view.y = [self navBarBottom] + self.titlesView.height + 40 * px;//tableview的高度默认为20
    vc.view.width = ScreenWidth;
    vc.view.height = scrollView.height;//tableview的底部
    //    //设置内边距
    //
    //    CGFloat bottom = self.tabBarController.tabBar.height;
    //    CGFloat top = CGRectGetMaxY(self.titlesView.frame);
    //    vc.tableView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
    //    //设置滚动条的内边距
    //    vc.tableView.scrollIndicatorInsets = vc.tableView.contentInset;
    [scrollView addSubview:vc.view];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //点击按钮
    int index = scrollView.contentOffset.x / scrollView.width;
    [self ButtonClick:self.titlesView.subviews[index]];
    
    
    // 添加子控制器的view
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

- (void)CreateNavigationBar{
    
    self.view.backgroundColor = backgroundcolor;
    naviagtionBar = [[CustomNavigationBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, [self navBarBottom])];
    [self.view addSubview:naviagtionBar];
    naviagtionBar.titleLabel.text = @"扫码活动";
    naviagtionBar.rightBtn.hidden = YES;
    naviagtionBar.rightBtnTitle.hidden = YES;
    [self.view addSubview:naviagtionBar];
    
    [naviagtionBar.leftBtn setImage:[UIImage imageNamed:@"左箭头"] forState:UIControlStateNormal];
    [naviagtionBar.leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:NO];
}
@end
